package com.batch_demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.batch_demo.util.HttpClientUtil;

import java.util.*;
import java.util.concurrent.*;

public class EvaluationDemo {

    public static void start() {
        ExecutorService executorService = Executors.newFixedThreadPool(100);

        List<FutureTask> list = new ArrayList(1);
        for (int i = 0; i < 1; i++) {
            FutureTask futureTask = new FutureTask(new Callable() {
                @Override
                public String call() throws Exception {
                    String url = "http://10.76.162.198:8080/api/operate/realTime/sync";
                    Map<String, String> map = new HashMap<>();
                    map.put("Accept", "application/json");
                    map.put("Accept-Encoding","gzip, deflate");
                    map.put("Accept-Language","zh-CN,zh;q=0.8,en;q=0.6");
                    map.put("Cache-Control","no-cache");
                    map.put("Connection","keep-alive");
                    map.put("Content-Type","application/json");
                    map.put("Cookie","uuid=eb191968c2354bb39eb5.1495781526.1.0.0; _ga=GA1.2.1799280315.1494236632; iuuid=6D098313884055CFD82718832AACEA9FD3AE5EFB78A3F141C7B1FB9F31B0D719; ci=10; cityname=%E4%B8%8A%E6%B5%B7; _lxsdk_cuid=160f78135c9c8-0bf0339fac1383-32677403-1fa400-160f78135c9c8; i_extend=H__a100040__b1; webp=1; __utma=74597006.1799280315.1494236632.1515982022.1515982022.1; __utmz=74597006.1515982022.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); isid=24DC246CEAAB798F455C3014B41BE647; oops=Kw2PC5UfGeP0jVqZv71Qo0yVE2wAAAAARQUAAM3ObsMMZixakxkGqcq-pUwqEtrMDWqSW_3Kktb2Z7gHa7hE05kwvr4l6_O9mBCRcA; logintype=fast; gh_c3a4f74aa194_zp_user_uid=c5551b1285c54df7839ca3d8f9fe4b88; hiring-campus-portal_sso_id=c5551b1285c54df7839ca3d8f9fe4b88");
                    map.put("Host","ampus.meituan.com");
                    map.put("Origin","https://campus.meituan.com");
                    map.put("Pragma","no-cache");
                    map.put("Referer","https://campus.meituan.com/resume-guide");
                    map.put("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
                    map.put("X-Requested-With","XMLHttpRequest");
                    Map<String, String> paramMap = new HashMap<>();
                    paramMap.put("tenantId", "1");
                    paramMap.put("id", "62");
                    JSONObject json = HttpClientUtil.postForJson(url, JSON.toJSONString(paramMap), map);
                    if (null == json) {
                        return null;
                    }
                    return json.toJSONString();
                }
            });
            executorService.submit(futureTask);
            list.add(futureTask);
        }

        for (int i = 0; i < list.size(); i ++) {
            try {
                Object o = list.get(i).get();
                if (null == o) {
                    System.err.println("第"+i+"个请求，报错了，很有可能超时了");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        executorService.shutdown();

    }

    public static void startKaoshi() {
        ExecutorService executorService = Executors.newFixedThreadPool(100);

        List<FutureTask> list = new ArrayList(10000);
        for (int i = 0; i < 10000; i++) {
            FutureTask futureTask = new FutureTask(new Callable() {
                @Override
                public String call() throws Exception {
                    String url = "https://kaoshi.sankuai.com/api/v1/candidates/exams/186";
                    Map<String, String> map = new HashMap<>();
                    map.put("Accept", "*/*");
                    map.put("Accept-Encoding","gzip, deflate, br");
                    map.put("Accept-Language","zh-CN,zh;q=0.8,en;q=0.6");
                    map.put("Cache-Control","no-cache");
                    map.put("Connection","keep-alive");
                    map.put("Content-Type","application/json; charset=utf-8");
                    map.put("Cookie","_lxsdk=159af83b378c8-030837d35c5ac8-1d326f50-13c680-159af83b378c8; uuid=6145e7c0-fa84-4a31-bd71-0595db63b06d; _jzqa=1.2445683492510392300.1499683048.1499683048.1499683048.1; _lxsdk_cuid=15dac0430ffc8-0875b92bb79e33-30657808-1fa400-15dac043100c8; uu=6145e7c0-fa84-4a31-bd71-0595db63b06d; ai=1; skmtutc=LshEMwxtz1uyFd1rdlZ430WmlWbpf3a8eHhmQb4juuCnugl1xzzENNDnWlx+ften4WYGI1xKqAnNOatp0hw3UA==-+oNbB5dvNeUMZZzhdnomC65ZHS4=; _ga=GA1.2.388097945.1485054719; originalUrl=/; SSO_SID=fe30dc124f*74de7b628d8733326c480; __mta=88969636.1508464859531.1508838284487.1508838294644.8; _lxsdk_s=15f4dc51934-b7d-ac6-168%7C%7C12");
                    map.put("Host","kaoshi.sankuai.com");
                    map.put("Pragma","no-cache");
                    map.put("Referer","https://kaoshi.sankuai.com/examinee/exam/186/intro");
                    map.put("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
                    map.put("X-Requested-With","XMLHttpRequest");
                    JSONObject json = HttpClientUtil.getJson(url, map);
                    if (null == json) {
                        return null;
                    }
                    return json.toJSONString();
                }
            });
            executorService.submit(futureTask);
            list.add(futureTask);
        }

        for (int i = 0; i < list.size(); i ++) {
            try {
                Object o = list.get(i).get();
                if (null == o) {
                    System.err.println("第"+i+"个请求，报错了，很有可能超时了");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        executorService.shutdown();

    }

    public static void main(String[] args) {
//        while (true) {
            startKaoshi();
//        }
    }

}
